package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.entity.vo;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-02-13
 */
@Slf4j
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;


    @GetMapping("/message1")
    public String mseeage1(){
        return "message1";
    }


    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items" , list).message("获取讲师列表成功");
    }

    @ApiOperation(value = "根据id删除讲师 - value" , notes = "根据id1删除讲师-notes")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师id" , required = true) @PathVariable String id) {
        // 删除头像
        teacherService.removeAvatarById(id);
        // 删除讲师
        boolean result = teacherService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在 ， 删除失败");
        }
    }


    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码")@PathVariable Integer page ,
                      @ApiParam(value = "每页显示条数")@PathVariable Integer limit  ,
                      @ApiParam(value = "讲师列表查询") TeacherQuery teacherQuery){
        IPage<Teacher> list = teacherService.selectPage(page , limit , teacherQuery);
        return R.ok().data("pageModel" , list);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象" , required = true)@RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }


    @ApiOperation(value = "更新讲师")
    @PutMapping("update")
    public R update(@ApiParam(value = "讲师对象" , required = true)@RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师id" , required = true)@PathVariable String id){
        Teacher teacher = teacherService.getById(id);

        try {
            int i = 1/0;
        } catch (Exception e) {
            System.out.println("1111111111111111111111");
           // log.error(e.getMessage());
             log.error(ExceptionUtils.getStackTrace(e));
            System.out.println("22222222222222222222222");
        }
        if (teacher != null){
            return R.ok().data("item" , teacher);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "讲师id列表" ,required = true) @RequestBody List<String> ids){
        boolean result = teacherService.removeByIds(ids);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    // 根据左关键字查询讲师姓名列表
    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@ApiParam(value = "查询关键字" , required = true) @PathVariable("key") String key){
        List<Map<String , Object>> teachers = teacherService.selectNameListByKey(key);
        return R.ok().data("items" , teachers);
    }


    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        R test = ossFileService.test();
        return R.ok().message(test.getMessage());
    }

}

